﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
namespace Data
{
    public class TheLoaiRepository
    {        
        public static List<TheLoaiDTO> GetTheLoaiList()
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = from ls in context.TheLoaiList
                        select new TheLoaiDTO { 
                            MaTheLoai=(int)ls.MaTheLoai,
                            TenTheLoai=ls.TenTheLoai
                        };
           
           
            return query.ToList();
            

        }
        
        public static TheLoaiDTO GetTheLoaiByMaTheLoai(int maTheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = (from tl in context.TheLoaiList
                         where tl.MaTheLoai == maTheLoai
                         select new TheLoaiDTO()
                         {
                             MaTheLoai=tl.MaTheLoai,
                             TenTheLoai=tl.TenTheLoai
                         }).FirstOrDefault();
            return query;
        }
       
        public static bool CheckDelete(int maTheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var count = (from s in context.SachList
                         where s.MaTheLoai==maTheLoai
                         select s).Count();
            if (count!=0)
            {
                return false;
            }
            return true;
        }
        
        public static void Delele(int ma)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ls in context.TheLoaiList
                     where ls.MaTheLoai == ma
                     select ls).First();
            
                context.DeleteObject(q);
                context.SaveChanges();
            
        }
        public static void Update(TheLoaiDTO theLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ls in context.TheLoaiList
                     where ls.MaTheLoai == theLoai.MaTheLoai
                     select ls).First();
            q.TenTheLoai = theLoai.TenTheLoai;
            context.SaveChanges();
        }
        public static void Insert(TheLoaiDTO theLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            TheLoai tl = new TheLoai()
            {
                MaTheLoai = theLoai.MaTheLoai,
                TenTheLoai = theLoai.TenTheLoai                
                
            };
            context.AddToTheLoaiList(tl);
            context.SaveChanges();
        }
        public static int GetCurrentID()
        {
            NhaSachEntities context = new NhaSachEntities();
            Nullable<int> id = context.GetCurrentID("TheLoai").SingleOrDefault();
            return (int) id;
        }
    }
}
